import themeMixin from '@/mixins/theme'
import menuMixin from '@/mixins/menu'
import Scene from 'Comps/Scene/app'
import {
  filterMenu,
  handleCustomMenu,
  handleSystemMenu
} from '@/utils/menu'
import $API from '@/api/services'

export default {
  name: 'menuBar',
  mixins: [themeMixin, menuMixin],
  components: {
    Scene
  },
  data() {
    return {
      isLanjiukj: $globalconfig.MODULES.IS_LANJIUKJ_PROGRAM_OPEN,
      homeFlag: $globalconfig.HomeFlag,
      curIndex: 0,
      isShowSubmenu: false
    }
  },
  async mounted() {
    this.menuDataList = await filterMenu(this.$CONFIG.defMenu)
  },
  computed: {
    getNavMenu() {
      if (this.menuData && this.menuData.navmenu && this.menuData.navmenu.length) {
        return this.menuData.navmenu
      }
    },
    getNavMenuCountStr() {
      let count = 0
      if (this.menuData && this.menuData.navmenu && this.menuData.navmenu.length) {
        this.menuData.navmenu.map(item => {
          count = count + item.title.length
        })
        // console.log(count)
        if (count > 22) {
          return false
        }
      }
      return true
    },
  },
  methods: {
    showBottomMenu() {
      this.formatActions(this.getNavMenu, this.handleBtMenu)
      this.handleSheet()
    },
    goHome() {
      if (window.navigator.userAgent.toLowerCase().indexOf('micromessenger') == -1) {
        window.location.href = $globalconfig.URLS.HOME
      }else {
        wx.miniProgram.getEnv(res => {
          if (res.miniprogram) {
            wx.miniProgram.switchTab({url: '/pages/index/index'})
          } else {
            window.location.href = $globalconfig.URLS.HOME
          }
        })
      }
    },
    hideSubMenu() {
      this.isShowSubmenu = false
    },
    handleNavMenu(item, index) {
      if(item.children && item.children.length) {
        if(index == this.curIndex) {

          this.isShowSubmenu = !this.isShowSubmenu
        } else {
          this.isShowSubmenu = true
        }
        // if (this.isLanjiukj && this.getNewTextShow(item)) {
        //   this.$store.dispatch('setNavmenuNew', {level: 0, id: item.menu_id || item.id})
        //   $API('sendMenuClick', [item.menu_id || item.id])
        // }
      } else {
        this.handleSubMenu(item)
      }
      this.curIndex = index
    },
    getNewTextShow(item) {
      if (!item.is_menu_watch) {
        return true
      }
      return false
    },
    handleSubMenu(item) {
      this.isShowSubmenu = false
      handleCustomMenu(item)
      if (this.isLanjiukj && this.getNewTextShow(item)) {
        this.$store.dispatch('setNavmenuNew', {level: item.level, id: item.menu_id || item.id})
        $API('sendMenuClick', [item.menu_id || item.id])
      }
    }
  }
}